import { DeleteRoom, UpdateRoom, CreateRoom, Get,GetAll,GetRoomPrice,GetRoomPriceSetUp,SaveRoomPrice,} from '@/services/room';

export default {
  namespace: 'room',

  state: {
    data: {
      result:{
        items:[],
      }
    },
    values:{
      roomTags:[],
      roomPictures:[],
    },
    prices:{
      name: "",
      defaultPrice: 0,
      weekendPrice: 0,
      months:{}
    }
  },

  effects: {
    *DeleteRoom({ payload,callback }, { call }) {
      const response = yield call(DeleteRoom, payload);
      if(callback){
        callback(response);
      }
    },
    *UpdateRoom({ payload,callback }, { call }) {
      const response = yield call(UpdateRoom, payload);
      if(callback){
        callback(response);
      }
    },
    *CreateRoom({ payload,callback }, { call }) {
      const response = yield call(CreateRoom, payload);
      if(callback){
        callback(response);
      }
    },
    /**
     *
     */
    *Get({ payload,callback }, { call,put }) {
      const response = yield call(Get, payload);
      if(callback){
        callback(response);
      }
      yield put({
        type:"GetInfo",
        payload:response,
      })
    },
    /**
     *
     */
    *GetAll({ payload,callback }, { call,put }) {
      const response = yield call(GetAll, payload);
      yield put({
        type:"GetAllInfo",
        payload:response
      });
      if(callback){
        callback(response);
      }
    },
    *GetRoomPrice({ payload,callback }, { call }) {
      const response = yield call(GetRoomPrice, payload);
      if(callback){
        callback(response);
      }
    },
    *GetRoomPriceSetUp({ payload,callback }, { call,put }) {
      const response = yield call(GetRoomPriceSetUp, payload);
      yield put({
        type:"GetRoomPriceSetUpInfo",
        payload:response
      });
      if(callback){
        callback(response);
      }
    },
    *SaveRoomPrice({ payload,callback }, { call }) {
      const response = yield call(SaveRoomPrice, payload);
      if(callback){
        callback(response);
      }
    },
  },

  reducers: {
    GetAllInfo(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    GetInfo(state, action) {
      return {
        ...state,
        values: action.payload.result,
      };
    },
    GetRoomPriceSetUpInfo(state, action) {
      return {
        ...state,
        prices: action.payload.result,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
  },
};
